import 'package:flutter/material.dart';
import 'package:getx_scaffold/getx_scaffold.dart';
import 'package:oramemo/core/repositories/default_api.dart';
import 'package:oramemo/entity/base_entity.dart';
import 'package:oramemo/generated/assets.dart';
import 'package:oramemo/pages/facility/devices/device_info_page.dart';
import 'package:oramemo/pages/facility/devices/ercode_not_device.dart';
import 'package:oramemo/pages/facility/facility_router.dart';

class DeviceAddCard extends StatelessWidget {
  const DeviceAddCard({super.key});

  @override
  Widget build(BuildContext context) {
    return Container(
      height: 180.w,
      margin: EdgeInsets.only(left: 16.w, right: 16.w),
      decoration: ShapeDecoration(
        color: const Color(0xFFF3F5F9),
        shape: RoundedRectangleBorder(
          side: const BorderSide(width: 1, color: Color(0x1992B9FF)),
          borderRadius: BorderRadius.circular(36.r),
        ),
      ),
      child: Row(
        mainAxisAlignment: MainAxisAlignment.spaceEvenly,
        children: [
          // 绑定时光机选项
          InkWell(
            onTap: () async {
              String code =
                  await Get.toNamed(FacilityRouter.scanningPage);
                if (code.contains('sn=')) {
                  final devCode = code.substring(code.indexOf('sn=') + 3);
                  Get.to(const DeviceInfoPage(), arguments: devCode);
                } else {
                  String recode = '识别到不是设备激活二维码:\n$code';
                  Get.to(const ErcodeNotDevice(), arguments: recode);
                }
            },
            child: Column(
              mainAxisAlignment: MainAxisAlignment.center,
              children: [
                IconX.image(
                  Assets.facilityBandS1,
                  width: 32.w,
                  height: 32.w,
                ),
                SizedBox(height: 11.h),
                Text(
                  '绑定时光机',
                  textAlign: TextAlign.center,
                  style: TextStyle(
                    color: Color(0xFF454655),
                    fontSize: 14.sp,
                    fontFamily: 'Inter',
                    fontWeight: FontWeight.w400,
                  ),
                ),
              ],
            ),
          ),

          // 加入时光机圈子选项
          InkWell(
            onTap: () async {
              String code =
                  await Get.toNamed(FacilityRouter.scanningPage);
              //invite:deviceId=111&userName=111
                if (code.startsWith('invite:')) {
                  final deviceId = code.substring(code.indexOf('deviceId=') + 9, code.indexOf('&userName='));
                  final userName = code.substring(code.indexOf('userName=') + 9);
                  final res = await DefaultApi()
                      .oraDeviceUserCircleScaninPost<BaseEntity>(
                          deviceId: deviceId, userName: userName,isShowLoading: true);
                  if (res?.isSuccess == true) {
                    Toast.show('加入成功');
                  } else {
                    Toast.show(res?.msg??'加入失败');
                  }
                } else {
                  String recode = '识别到不是邀请二维码:\n$code';
                  Get.to(const ErcodeNotDevice(), arguments: recode);
                }
            },
            child: Column(
              mainAxisAlignment: MainAxisAlignment.center,
              children: [
                IconX.image(
                  Assets.facilityAddS1,
                  width: 32.w,
                  height: 32.w,
                ),
                SizedBox(height: 11.h),
                Text(
                  '加入时光机圈子',
                  textAlign: TextAlign.center,
                  style: TextStyle(
                    color: Color(0xFF454655),
                    fontSize: 14.sp,
                    fontFamily: 'Inter',
                    fontWeight: FontWeight.w400,
                  ),
                ),
              ],
            ),
          ),
        ],
      ),
    );
  }
}
